package com.gopay.common.rcs.dao.antiphish.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.gopay.common.base.dao.impl.BaseDAOImpl;
import com.gopay.common.domain.rcs.RcsAntiPhishViolateLog;
import com.gopay.common.rcs.bean.AntiPhishType;
import com.gopay.common.rcs.bean.IllegalReqDicision;
import com.gopay.common.rcs.bean.UserDicision;
import com.gopay.common.rcs.dao.antiphish.RcsAntiPhishingViolateLogDAO;
import com.gopay.common.util.StringTools;

/**
 * 触犯防钓鱼规则
 * 
 * @RcsAntiPhishingViolateLogDAO.java
 * @author liujy
 * @2013-7-23 下午6:38:18 www.gopay.com.cn Inc.All rights reserved.
 */
@Repository("rcsAntiPhishingViolateLogDAO")
public class RcsAntiPhishingViolateLogDAOImpl extends BaseDAOImpl<RcsAntiPhishViolateLog, Serializable> implements
        RcsAntiPhishingViolateLogDAO {

    /* 查询列表
     * @see com.gopay.common.rcs.dao.antiphish.RcsAntiPhishingViolateLogDAO#findAntiPhishLog(java.util.Map, int, int)
     */
    @Override
    public List<RcsAntiPhishViolateLog> findAntiPhishLog(final Map<String, Object> parameterMap, final int pageIndex,
            final int pageSize) {
        final StringBuffer sqlSb = getSql(parameterMap);
        List<?> objectArrayList = getListObjectArray(parameterMap, pageIndex, pageSize, sqlSb);
        return buidVoList(objectArrayList);
    }

    /**
     * 构建Vo对象 其中//用商户Id位置 存商户Email，用国付宝订单号位置存交易时间
     * 
     * @param objectArrayList
     * @return
     */
    private List<RcsAntiPhishViolateLog> buidVoList(List<?> objectArrayList) {
        List<RcsAntiPhishViolateLog> logList = new ArrayList<RcsAntiPhishViolateLog>();
        for (int i = 0, size = null == objectArrayList ? 0 : objectArrayList.size(); i < size; i++) {
        	Object[] obj = (Object[])objectArrayList.get(i);
            RcsAntiPhishViolateLog log = new RcsAntiPhishViolateLog();
            log.setAcctId((String) obj[0]);
            log.setAntiPhishType(getAntiPhishType((String)obj[1]));
            log.setIllegalReqDicision(getIllegalReqDicision((String) obj[2],(String)obj[8]));
            log.setMerOrderId((String) obj[3]);
            log.setGopayOutOrderId(StringTools.checkNulltoString(obj[4]));
            log.setMerName(StringTools.checkNulltoString(obj[5]));
            log.setMerId((String) obj[6]); // 用商户Id位置 存商户Email
            log.setGopayOrderId((String) obj[7]);// 用国付宝订单号位置存 交易时间
            logList.add(log);
        }

        return logList;
    }


    /**
     * 处理类型
     * @param illegalReqDicisionCode
     * @return
     */
    private String getIllegalReqDicision(String illegalReqDicisionCode,String userDicisionCode){
        String illegalReqDicision = null;
        
        if (illegalReqDicisionCode.equals("01")) {
            illegalReqDicision = "系统拒绝";
        } else if (userDicisionCode==null||userDicisionCode.equals(UserDicision.UNHANDLE.code)) {
            illegalReqDicision = "用户未处理";
        }else if (userDicisionCode.equals(UserDicision.STOP.code)) {
            illegalReqDicision = "用户拒绝";
        }  else if (userDicisionCode.equals(UserDicision.CONTINUE.code)) {
            illegalReqDicision = "用户通过";
        }
        return illegalReqDicision;
    }
    
    /**
     * 翻译触犯类型
     * @param antiPhishTypeCode
     * @return
     */
    private String getAntiPhishType(String antiPhishTypeCode){
        String antiPhishType=null;
        if (antiPhishTypeCode.equals(AntiPhishType.DOMAINNAME_EXACT.code)) {
            antiPhishType = "域名不匹配";/*精确域名校验*/
        } else if (antiPhishTypeCode.equals(AntiPhishType.DOMAINNAME_FUZZY.code)) {
            antiPhishType = "域名不匹配";/*域名模糊校验*/
        } else if (antiPhishTypeCode.equals(AntiPhishType.TIMESTAMP.code)) {
            antiPhishType = "时间戳超时";
        } else if (antiPhishTypeCode.equals(AntiPhishType.TRAN_IP.code)) {
            antiPhishType = "订单IP校验不一致";
        } else if (antiPhishTypeCode.equals(AntiPhishType.VERIFY_CODE.code)) {
            antiPhishType = "验证码提示页";
        } else if (antiPhishTypeCode.equals(AntiPhishType.IP_WHITE.code)) {
            antiPhishType = "IP白名单不符";
        }
        return antiPhishType;
    }

    private List<?> getListObjectArray(final Map<String, Object> parameterMap, final int pageIndex,
            final int pageSize, final StringBuffer sqlSb) {
    	return findBySql(sqlSb.toString(), parameterMap, pageIndex, pageSize);
    }

    private StringBuffer getSql(final Map<String, Object> parameterMap) {
        final StringBuffer sqlSb = new StringBuffer();
        sqlSb.append("select ")
                .append("    r.ACCT_ID,                 ")//0
                .append("    r.ANTI_PHISH_TYPE,         ")//1
                .append("    r.ILLEGAL_REQ_DICISION,    ")//2
                .append("    r.MER_ORDER_ID,            ")//3
                .append("    r.GOPAY_OUT_ORDER_ID,      ")//4
                .append("    r.MER_NAME,                ")//5
                .append("    c.EMAIL ,                  ")//6
                .append("    to_char(r.MER_TXN_TM,'YYYY-MM-DD hh24:mi') ,")//7
                .append("    r.USER_DICISION    ")//8
                .append("   from RCS_ANTI_PHISH_VIOLATE_LOG r left join CUST_CORP_INFO c on r.MER_ID=c.cust_id where 1=1 ");
        sqlSb.append(buidWhere(parameterMap));
        sqlSb.append(" order by MER_TXN_TM desc ");
        return sqlSb;
    }

    @Override
    public int getCount(final Map<String, Object> parameterMap) {
        StringBuffer sql = new StringBuffer();
        sql.append("select count(*) ").append("    from RCS_ANTI_PHISH_VIOLATE_LOG r ")
                .append("  left join CUST_CORP_INFO c            ").append("  on r.MER_ID=c.cust_id where 1=1       ");
        sql.append(buidWhere(parameterMap));
        return findCountBySql(sql.toString(), parameterMap);
    }

    private Object buidWhere(Map<String, Object> parameterMap) {
        StringBuffer whereSQL = new StringBuffer();

        if (StringUtils.isNotBlank((String) parameterMap.get("startTranDate"))) {
            whereSQL.append("   and   to_char(r.MER_TXN_TM,'YYYY-MM-DD') >=:startTranDate");
        }
        if (StringUtils.isNotBlank((String) parameterMap.get("endTranDate"))) {
            whereSQL.append("   and   to_char(r.MER_TXN_TM,'YYYY-MM-DD') <=:endTranDate");
        }

        if (StringUtils.isNotBlank((String) parameterMap.get("antiPhishType"))) {
            if(parameterMap.get("antiPhishType").equals("01")){
                whereSQL.append("   and   r.ANTI_PHISH_TYPE in ('01','02')");
                parameterMap.remove("antiPhishType");
            }else{
                
                whereSQL.append("   and   r.ANTI_PHISH_TYPE =:antiPhishType");
            }
        }
        if (StringUtils.isNotBlank((String) parameterMap.get("illegalReqDicision"))) {
            if(parameterMap.get("illegalReqDicision").equals("00")){
                whereSQL.append("   and   r.ILLEGAL_REQ_DICISION = '")
                        .append(IllegalReqDicision.REJECTION.code).append("'");
                parameterMap.remove("illegalReqDicision");
            }else{
                whereSQL.append("   and  r.ILLEGAL_REQ_DICISION = '")
                .append(IllegalReqDicision.USER_DECIDE.code).append("' ");
                if(StringTools.checkNulltoString(parameterMap.get("illegalReqDicision")).equals("03")){
                	whereSQL.append(" and  (r.USER_DICISION =:illegalReqDicision or r.USER_DICISION is null )");
                }else{
                	
                	whereSQL.append(" and  r.USER_DICISION =:illegalReqDicision ");
                }
            }
            
        }
        if (StringUtils.isNotBlank((String) parameterMap.get("merOrderId"))) {
            whereSQL.append("   and   r.MER_ORDER_ID =:merOrderId");
        }
        if (StringUtils.isNotBlank((String) parameterMap.get("gopayOutOrderId"))) {
            whereSQL.append("   and  r.GOPAY_OUT_ORDER_ID =:gopayOutOrderId");
        }
        if (StringUtils.isNotBlank((String) parameterMap.get("merName"))) {
            whereSQL.append("   and  r.MER_NAME like '%").append(parameterMap.get("merName")).append("%' ");
            parameterMap.remove("merName");
        }
        if (StringUtils.isNotBlank((String) parameterMap.get("merEmail"))) {
            whereSQL.append("   and  c.email =:merEmail");
        }
        if (StringUtils.isNotBlank((String) parameterMap.get("acctId"))) {
            whereSQL.append("   and   r.ACCT_ID =:acctId");
        }
        return whereSQL.toString();
    }

    @Override
    public RcsAntiPhishViolateLog getByTransDtlSq(int transDtlSq) {
        String hql = " from RcsAntiPhishViolateLog t where t.transDtlSq = " + transDtlSq;
        List<RcsAntiPhishViolateLog> ss = findByHql(hql);
        return  null == ss || 0 == ss.size() ? null : ss.get(0);
    }

}